package com.yjzx.core.engine;

import com.yjzx.core.intercept.CustomIntercept;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

/**
 * @author guozhi
 * @date 2023/10/18
 * @description 自定义引擎
 */
public class CustomEngine implements Serializable {
    private static Configuration configuration;


    /***
     * 获取FreeMaker创建模板
     */
    public static Configuration getConfiguration() {
        if (configuration == null) {
            configuration = new Configuration(Configuration.VERSION_2_3_23);
            try {
                configuration.setTemplateLoader(new ClassTemplateLoader(CustomEngine.class, "/templates/"));
                configuration.setNumberFormat("#");
                configuration.setClassicCompatible(true);
                configuration.setDefaultEncoding("UTF-8");
                configuration.setLocale(Locale.CHINA);
            } catch (Exception e) {
                e.printStackTrace();
            }
            configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
        }
        return configuration;
    }

    /**
     * 代码生成引擎的执行器
     *
     * @param list
     */
    public static void handleCustom(List<CustomIntercept> list) {
        for (CustomIntercept customIntercept : list) {
            customIntercept.handle();
        }
    }

}
